package javaStu.Cache;

import com.google.common.cache.*;
import com.google.common.collect.Maps;

import java.util.Map;

public class CacheTest {
    public static void main(String[] args) throws Exception {
        Map<String, Integer> map = Maps.newHashMap();
        map.put("2020-02-17", 1);
        map.put("2020-02-18", 2);
        map.put("2020-02-19", 3);
        map.put("2020-02-20", 4);

        LoadingCache<String, Integer> dimDateLoadingCache = CacheBuilder.newBuilder()
                .maximumSize(3)
                .recordStats()
                .removalListener(new RemovalListener<String, Integer>() {
                    @Override
                    public void onRemoval(RemovalNotification<String, Integer> notification) {
                        System.out.println("dimDateLoadingCache remove: " + notification.getKey());
                    }
                })
                .build(new CacheLoader<String, Integer>() {
                    @Override
                    public Integer load(String date) throws Exception {
                        System.out.println("重新load : " + date);
                        return map.get(date);
                    }
                });
        Integer value1 = dimDateLoadingCache.get("2020-02-17");
        System.out.println(value1);
        Integer value2 = dimDateLoadingCache.get("2020-02-18");
        System.out.println(value2);
        Integer value3 = dimDateLoadingCache.get("2020-02-19");
        System.out.println(value3);
        Integer value4 = dimDateLoadingCache.get("2020-02-20");
        System.out.println(value4);
        System.out.println(dimDateLoadingCache.get("2020-02-17"));

    }
}
